<html>
<head>
  <script src="../../OLLoader.js"></script>
  <script type="text/javascript">

    function test_initialize(t) {
        t.plan(1);

        var format = new OpenLayers.Format.WFST.v1_1_0({});
        t.ok(format instanceof OpenLayers.Format.WFST.v1_1_0, "constructor returns instance");
    }
    
    function test_read(t) {
        t.plan(3);
        
        var data = readXML("TransactionResponse");        
        var format = new OpenLayers.Format.WFST.v1_1_0({
            featureNS: "http://www.openplans.org/topp",
            featureType: "states"
        });
        var result = format.read(data);
        t.eq(result.insertIds[0], "parcelle.40", "First InsertId read correctly");
        t.eq(result.insertIds[1], "parcelle.41", "Second InsertId read correctly");
        t.eq(result.success, true, "Success read correctly");
    }

    function test_read_hits(t) {
        t.plan(1);
        var data = readXML("NumberOfFeatures");
        var format = new OpenLayers.Format.WFST.v1_1_0({
            featureNS: "http://mapserver.gis.umn.edu/mapserver",
            featureType: "AAA64"
        });
        var result = format.read(data, {output: "object"});
        t.eq(result.numberOfFeatures, 625, "numberOfFeatures of FeatureCollection correctly read");
    }

    function test_read_boundedBy(t) {
        t.plan(4);
        var data = readXML("boundedBy");
        var format = new OpenLayers.Format.WFST.v1_1_0({
            featureNS: "http://mapserver.gis.umn.edu/mapserver",
            featureType: "AAA212"
        });
        var result = format.read(data, {output: "object"});
        var bounds = result.bounds;
        t.eq(bounds.left.toFixed(3), '3197.880', "Left bounds of the feature collection correctly parsed");
        t.eq(bounds.bottom.toFixed(3), '306457.313', "Bottom bounds of the feature collection correctly parsed");
        t.eq(bounds.right.toFixed(3), '280339.156', "Right bounds of the feature collection correctly parsed");
        t.eq(bounds.top.toFixed(3), '613850.438', "Top bounds of the feature collection corectly parsed");
    }

    function test_write(t) {

        var format = new OpenLayers.Format.WFST.v1_1_0({
            featureNS: "http://www.openplans.org/topp",
            featureType: "states",
            featurePrefix: "topp",
            srsName: "urn:ogc:def:crs:EPSG::4326",
            geometryName: "the_geom"
        });

        var cases = [{
            id: "query0",
            writer: "wfs:Query",
            arg: {
                filter: new OpenLayers.Filter.Spatial({
                    type: OpenLayers.Filter.Spatial.BBOX,
                    value: new OpenLayers.Bounds (1,2,3,4)
                })
            }
        }, {
            id: "getfeature0",
            writer: "wfs:GetFeature",
            arg: {
                resultType: "hits",
                propertyNames: ["STATE_NAME", "STATE_FIPS", "STATE_ABBR"]
            }
        }, {
            id: "getfeature1",
            writer: "wfs:GetFeature",
            arg: {
                count: 10,
                startIndex: 20
            }
        }];
        
        t.plan(cases.length);
        
        var test, got, exp;
        for(var i=0; i<cases.length; ++i) {
            test = cases[i];
            exp = readXML(test.id);
            got = format.writeNode(test.writer, test.arg);
            t.xml_eq(got, exp, test.id + ": correct request");
        }
    }

    function test_write_poorconfig(t) {
        t.plan(1);
        var format = new OpenLayers.Format.WFST.v1_1_0({
            featureType: "states",
            featurePrefix: "topp"
        });
        var exp = "topp:states";
        var got = format.writeNode("wfs:Query").getAttribute("typeName");
        t.eq(got, exp, "Query without featureNS but with featurePrefix queries for the correct featureType");
    }

    var xmlFormat = new OpenLayers.Format.XML();
    function readXML(id) {
        var xml = document.getElementById(id).firstChild.nodeValue;
        return xmlFormat.read(xml).documentElement;        
    }

  </script>
</head>
<body>
<div id="map" style="width:512px; height:256px"> </div>
<div id="NumberOfFeatures"><!--
<?xml version='1.0' encoding="ISO-8859-1" ?>
<wfs:FeatureCollection
   xmlns:rws="http://mapserver.gis.umn.edu/mapserver"
   xmlns:gml="http://www.opengis.net/gml"
   xmlns:wfs="http://www.opengis.net/wfs"
   xmlns:ogc="http://www.opengis.net/ogc"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://mapserver.gis.umn.edu/mapserver http://intranet.rijkswaterstaat.nl/services/geoservices/nwb_wegen?SERVICE=WFS&amp;VERSION=1.1.0&amp;REQUEST=DescribeFeatureType&amp;TYPENAME=feature:AAA64&amp;OUTPUTFORMAT=text/xml; subtype=gml/3.1.1  http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd" numberOfFeatures="625">
</wfs:FeatureCollection>
--></div>
<div id="TransactionResponse"><!--
<wfs:TransactionResponse version="1.1.0" xmlns:ogc="http://www.opengis.net/ogc" xmlns:tiger="http://www.census.gov" xmlns:wfs="http://www.opengis.net/wfs" xmlns:topp="http://www.openplans.org/topp" xmlns:sf="http://www.openplans.org/spearfish" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink">
    <wfs:TransactionSummary>
        <wfs:totalInserted>0</wfs:totalInserted>
        <wfs:totalUpdated>1</wfs:totalUpdated>
        <wfs:totalDeleted>0</wfs:totalDeleted>
    </wfs:TransactionSummary>
    <wfs:TransactionResults/>
    <wfs:InsertResults>
        <wfs:Feature>
            <ogc:FeatureId fid="parcelle.40"/>
        </wfs:Feature>
        <wfs:Feature>
            <ogc:FeatureId fid="parcelle.41"/>
        </wfs:Feature>
    </wfs:InsertResults>
</wfs:TransactionResponse>
--></div>
<div id="query0"><!--
<wfs:Query xmlns:wfs="http://www.opengis.net/wfs" typeName="topp:states" srsName="urn:ogc:def:crs:EPSG::4326" xmlns:topp="http://www.openplans.org/topp">
    <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
        <ogc:BBOX>
            <ogc:PropertyName>the_geom</ogc:PropertyName>
            <gml:Envelope xmlns:gml="http://www.opengis.net/gml" srsName="urn:ogc:def:crs:EPSG::4326">
                <gml:lowerCorner>1 2</gml:lowerCorner>
                <gml:upperCorner>3 4</gml:upperCorner>
            </gml:Envelope>
        </ogc:BBOX>
    </ogc:Filter>
</wfs:Query>
--></div>
<div id="getfeature0"><!--
<wfs:GetFeature service="WFS" version="1.1.0" resultType="hits" xmlns:topp="http://www.openplans.org/topp"
                xmlns:wfs="http://www.opengis.net/wfs"
                xmlns:ogc="http://www.opengis.net/ogc"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd">
    <wfs:Query xmlns:wfs="http://www.opengis.net/wfs" typeName="topp:states" srsName="urn:ogc:def:crs:EPSG::4326" xmlns:topp="http://www.openplans.org/topp">
        <wfs:PropertyName>STATE_NAME</wfs:PropertyName>
        <wfs:PropertyName>STATE_FIPS</wfs:PropertyName>
        <wfs:PropertyName>STATE_ABBR</wfs:PropertyName>
    </wfs:Query>
</wfs:GetFeature>
--></div>
<div id="getfeature1"><!--
<wfs:GetFeature service="WFS" version="1.1.0" startIndex="20" count="10" xmlns:topp="http://www.openplans.org/topp"
                xmlns:wfs="http://www.opengis.net/wfs"
                xmlns:ogc="http://www.opengis.net/ogc"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd">
    <wfs:Query xmlns:wfs="http://www.opengis.net/wfs" typeName="topp:states" srsName="urn:ogc:def:crs:EPSG::4326" xmlns:topp="http://www.openplans.org/topp">
    </wfs:Query>
</wfs:GetFeature>
--></div>
<div id="boundedBy"><!--
<?xml version='1.0' encoding="ISO-8859-1" ?>
<wfs:FeatureCollection
   xmlns:rws="http://mapserver.gis.umn.edu/mapserver"
   xmlns:gml="http://www.opengis.net/gml"
   xmlns:wfs="http://www.opengis.net/wfs"
   xmlns:ogc="http://www.opengis.net/ogc"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://mapserver.gis.umn.edu/mapserver http://ontwikkel.intranet.rijkswaterstaat.nl/services/geoservices/ov_zonering?SERVICE=WFS&amp;VERSION=1.1.0&amp;REQUEST=DescribeFeatureType&amp;TYPENAME=AAA212&amp;OUTPUTFORMAT=text/xml; subtype=gml/3.1.1  http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd">
      <gml:boundedBy>
      	<gml:Envelope srsName="EPSG:28992">
      		<gml:lowerCorner>3197.880000 306457.313000</gml:lowerCorner>
      		<gml:upperCorner>280339.156000 613850.438000</gml:upperCorner>
      	</gml:Envelope>
      </gml:boundedBy>
    <gml:featureMember>

      <rws:AAA212 gml:id="AAA212.791">
        <gml:boundedBy>
        	<gml:Envelope srsName="EPSG:28992">
        		<gml:lowerCorner>196507.469000 502347.938000</gml:lowerCorner>
        		<gml:upperCorner>202430.844000 510383.719000</gml:upperCorner>
        	</gml:Envelope>
        </gml:boundedBy>
        <rws:geometry>

          <gml:MultiSurface srsName="EPSG:28992">
            <gml:surfaceMembers>
              <gml:Polygon>
                <gml:exterior>
                  <gml:LinearRing>
                    <gml:posList srsDimension="2">200448.047000 510383.719000 198475.031000 509253.875000 198477.422000 507339.688000 196507.469000 505841.969000 196507.625000 504980.281000 196621.359000 505029.969000 196825.328000 505114.000000 197310.031000 505183.469000 197636.609000 505148.750000 197837.594000 505061.563000 197941.031000 504953.688000 198003.094000 504817.719000 198023.781000 504721.688000 198016.391000 504597.531000 197907.234000 504363.219000 197716.734000 504013.969000 197700.156000 503567.563000 197775.531000 503373.969000 197930.688000 503153.781000 198034.234000 503045.594000 198170.078000 502932.125000 198504.047000 502725.250000 198858.719000 502550.875000 199138.000000 502460.719000 199336.000000 502347.938000 199044.125000 504910.969000 199549.359000 507065.781000 200280.594000 506878.938000 202430.844000 507474.625000 202430.844000 508850.906000 200448.047000 510383.719000 </gml:posList>
                  </gml:LinearRing>
                </gml:exterior>

              </gml:Polygon>
            </gml:surfaceMembers>
          </gml:MultiSurface>
        </rws:geometry>
        <rws:OBJECTID>791</rws:OBJECTID>
        <rws:HECTARES>1800.89</rws:HECTARES>
        <rws:ZONENR>4620</rws:ZONENR>

        <rws:NULZONES> </rws:NULZONES>
        <rws:AREA>0</rws:AREA>
        <rws:PERIMETER>24305.1</rws:PERIMETER>
      </rws:AAA212>
    </gml:featureMember>
</wfs:FeatureCollection>
--></div>
</body>
</html>
